﻿using DL.Animation;
using DL.Characters.Player.FSM.StateMachine;

namespace DL.Characters.Player.FSM.State
{
    /// <summary>
    /// 跳跃一系列状态基类
    /// </summary>
    public class PlayerOnAirState : PlayerMovementState
    {
        public PlayerOnAirState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(playerStateMachine,
            property)
        {
        }

        public override void Update()
        {
            // 1. 检查速度是否达到强降落标准  
            Animator.SetBool(AnimatorParameter.FastDown, MoveController.CurrentVerticalVelocity.Value < ReusableData.FastFallVelocity);
            StateTransition();
        }

        public override void FixedUpdate()
        {
            base.FixedUpdate();

            if (ReusableData.InputDirection.sqrMagnitude > .2f)
            {
                MoveController.CharacterMoveInterface(Self.forward,
                    ReusableData.InputDirection.sqrMagnitude * ReusableData.InputMult);
            }
            else
            {
                //沿用当前速度Debug.Log(2);
                // Debugger.EO_Log(MoveController.CurrentMovementVelocity.Value, Color.magenta);
                MoveController.CharacterMoveInterface(Self.forward, MoveController.CurrentMovementVelocity.Value);
            }
        }

        protected void StateTransition()
        {
            if (StateMachine.AnimatorStateInfo.IsName("Falling") &&
                !StateMachine.IsSameStateWithCurrentState<PlayerAirFallingState>() &&
                !StateMachine.AnimatorIsInTransition)
            {
                StateMachine.ChangeState<PlayerAirFallingState>();
            }
            else if (StateMachine.AnimatorStateInfo.IsName("JumpDown") &&
                     !StateMachine.IsSameStateWithCurrentState<PlayerJumpDownState>() &&
                     !StateMachine.AnimatorIsInTransition)
            {
                StateMachine.ChangeState<PlayerJumpDownState>();
            }
            else if (StateMachine.AnimatorStateInfo.IsName("FastJumpDown") &&
                     !StateMachine.IsSameStateWithCurrentState<PlayerFastJumpDownState>() &&
                     !StateMachine.AnimatorIsInTransition)
            {
                StateMachine.ChangeState<PlayerFastJumpDownState>();
            }
            else if (!ReusableData.IsJump && StateMachine.AnimatorStateInfo.IsTag(AnimatorTag.Motion) &&
                     !StateMachine.IsSameStateWithCurrentState<PlayerLocomotionState>() &&
                     !StateMachine.AnimatorIsInTransition)
            {
                StateMachine.ChangeState<PlayerLocomotionState>();
            }
        }
    }
}